﻿Imports System.Data.SqlClient

Public Class frmE_Roles
    Private Servicios As New DataTable("Servicios")
    Private AutocompletadoServicios As New AutoCompleteStringCollection

    Private Sub frmE_Roles_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Servicios.PrimaryKey = Nothing
        Servicios.Clear()
        Servicios.Columns.Clear()
    End Sub
    Private Sub frmE_Roles_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ToolStrip2.Cursor = Cursors.Hand
        ToolStrip2.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        CargaNombresServicios()
        Dim Claves(0) As DataColumn
        Claves(0) = Servicios.Columns("Descripcion")
        Servicios.PrimaryKey = Claves

        For Each Descripcion In Servicios.Rows
            AutocompletadoServicios.Add(Descripcion("Descripcion"))
        Next

        CargaRoles()

    End Sub

    Private Sub ToolStripButton10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton10.Click
        Try
            Call SetSqlCitra("DELETE FROM E_Roles")
            Dim Rol As New clsE_Roles
            For i = 0 To dgRoles.Rows.Count - 1
                Rol.Nombre = dgRoles.Rows(i).Cells(0).Value
                Rol.Salida = dgRoles.Rows(i).Cells(1).Value
                Rol.Llegada = dgRoles.Rows(i).Cells(2).Value
                Rol.TipoDia = UCase(Mid(dgRoles.Rows(i).Cells(3).Value, 1, 1))
                Rol.Encontrado = 0
                Rol.Insertar()
            Next

        Catch ex As Exception

        End Try

        Close()

    End Sub
    Private Sub dgResult_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgRoles.CellDoubleClick

        frmBuscaRoles.ShowDialog()


    End Sub

    Sub CargaNombresServicios()
        Servicios.Columns.Add("Descripcion")

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = OptocontrolConnection

        Dim strSql As String = ""
        strSql = "Select COD_RUTA "
        strSql = strSql & " From RUTA WHERE TIPO_TARIFA='Multitarifa'"
        strSql = strSql & " order by COD_RUTA"

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader
        Do While rdBuscar.Read()
            Servicios.Rows.Add(rdBuscar("COD_RUTA").ToString.Trim)
        Loop

    End Sub
    Private Sub ControlEdicion(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles dgRoles.EditingControlShowing
        If dgRoles.CurrentCell.ColumnIndex = 0 Then
            DirectCast(e.Control, TextBox).AutoCompleteMode = AutoCompleteMode.SuggestAppend
            DirectCast(e.Control, TextBox).AutoCompleteSource = AutoCompleteSource.CustomSource
            DirectCast(e.Control, TextBox).AutoCompleteCustomSource = AutocompletadoServicios
        Else
            DirectCast(e.Control, TextBox).AutoCompleteMode = Nothing
        End If
    End Sub
    Sub CargaRoles()
        dgRoles.Rows.Clear()

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim strSql As String = "SELECT * FROM E_Roles ORDER BY IdRol"
       

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader
        Do While rdBuscar.Read()
            dgRoles.Rows.Add(rdBuscar("Nombre"), rdBuscar("Salida"), rdBuscar("Llegada"), rdBuscar("TipoDia"))
        Loop

    End Sub

    Private Sub ToolStripButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton6.Click
        Close()

    End Sub
End Class